<?php
namespace app\models;
use yii;
use yii\db\ActiveRecord;


 class User extends ActiveRecord

 {

    public $rememberMe=true;
    public $repass;
 	public static function tableName(){

 		return "{{%user}}";
 	}
 	public function attributeLabels(){
        return [
            'username'=>'用户帐号',
            'useremail'=>'用户邮箱',
            'userpass'=>'用户密码',
            'repass'=>'确认密码',

        ];
    }
    public function rules(){
    	return [ 
        ['username','required','message'=>'用户帐号不能为空','on'=>['login','seekpass','changpass','useradd']],
        ['userpass','required','message'=>'用户密码不能为空','on'=>['login','changpass','useradd']],
        ['rememberMe','boolean','on'=>'login'],
        ['userpass','validatePass','on'=>'login'],
        ['useremail','required','message'=>'电子邮箱不能为空','on'=>['seekpass','useradd','regbymail']],
        ['useremail','unique','message'=>'电子邮箱已经被注册','on'=>['useradd','regbymail']],
        ['username','unique','message'=>'用户已经被注册','on'=>['useradd','regbymail']],
        ['useremail','email','message'=>'电子邮箱格式错误','on'=>['seekpass','regbymail']],
        ['useremail','validateEmail','on'=>'seekpass'],
        ['repass','required','message'=>'确认密码不能为空','on'=>['changpass','useradd']],
        ['repass','compare','compareAttribute'=>'userpass','message'=>'两次密码不一样','on'=>['changpass','useradd']],
    	 ];
        }
        public function validatePass(){

        	if(!$this->hasErrors()){
        		$data=self::find()->where('username =:user and userpass=:pass',[":user"=>$this->username,":pass"=>md5($this->userpass)])->one();
        		if(is_null($data)){
        			$this->addError("userpass","用户名或者密码错误");
        		}
        	}
        }
        public function validateEmail($data){
            if(!$this->hasErrors()){
                $data=self::find()->where('username=:user and useremail=:email',[':user'=>$this->username,':email'=>$this->useremail])->one();
                if(is_null($data)){
                    $this->addError("useremail","用户邮箱不匹配");
                }

            }

        }
        public function getProfile(){
        	return $this->hasOne(Profile::className(),['userid'=>'userid']);
        }
        public function login($data){
 	          $this->scenario='login';
 	          if($this->load($data)&&$this->validate()){
 	          	$lifetime=$this->rememberMe?24*3600:0;
 	          	$session=YII::$app->session;
 	          	session_set_cookie_params($lifetime);
 	          	$session['user']=[
 	          	'username'=>$this->username,
 	          	'isLogin'=>1,
 	          	];
 	          	$this->updateAll(['logintime'=>time(),'loginip'=>ip2long(YII::$app->request->userIP)],'username=:user',[':user'=>$this->username]);
 	          	return (bool)$session['user']['isLogin'];
 	          }else{

 	          	return false;
 	          }
        }
        public function seekPass($data){
        $this->scenario='seekpass';
        if($this->load($data)&&$this->validate()){
            $time=time();
            $token=$this->creatToken($data['User']['username'],$time);
            $mailer=Yii::$app->mailer->compose('seekpass',['username'=>$data['User']['username'],'time'=>$time,'token'=>$token]);
            $mailer->setFrom('15958615799@163.com');
            $mailer->setTo($data['User']['useremail']);
            $mailer->setSubject('华仔商城-找回密码');
            if($mailer->send()){
                return true;
            }

        } 
        return false;
        }

        public function regByMail($data){
        	$this->scenario='regbymail';
 
          // $data['User']['username']= Yii::$app->security->generateRandomString(9);
           //$data['User']['userpass']= Yii::$app->security->generateRandomString(8);

           $data['User']['username']= 'zf'.uniqid();
           $data['User']['userpass']= uniqid();

           if($this->adduser($data)){
            $time=time();
           // $token=$this->creatToken($data['User']['useremail'],$time);
            $mailer=Yii::$app->mailer->compose('senduserinfo',['User'=>$data['User']]);
            $mailer->setFrom('15958615799@163.com');
            $mailer->setTo($data['User']['useremail']);
            $mailer->setSubject('华仔商城-新用户注册');
            if($mailer->send()){
                return true;
            }

        
           }
  
          

        }


        public function changPass($data){
         
        $this->scenario='changpass';
       if($this->load($data) && $this->validate()){
         
        return (bool)$this->updateAll(['userpass'=>md5($this->userpass)],'username=:user',[':user'=>$data['User']['username']]);

        }
        return false;

        }

        public function reg($data){
            $this->scenario='useradd';
            
            if($this->load($data) && $this->validate()){

                $this->userpass=md5($this->userpass);
                //$this->repass=md5($this->repass);
                if($this->save(false)){
                    return true;
                }else{
                    return false;
                }

            }
        }
        public function adduser($data){
            $this->scenario='regbymail';
 
            if($this->load($data) && $this->validate()){

                $this->userpass=md5($this->userpass);
                //$this->repass=md5($this->repass);
                if($this->save(false)){
                    return true;
                }else{
                    return false;
                }

            }
        }


        public function creatToken($username,$time){
            return md5(md5($username).base64_encode(Yii::$app->request->userip).md5($time));
        }

         
    }
 
